<?php
/**
 * Description of ServerManager
 *
 * @author David Beaver
 * The Server Manager is an API for managing Execution Server.  A URL, 
 * communication port and ftp port will be necessary to communicate with the
 * servers.
 */
include("Server.php");

class ServerManager {
    //array of servers to manager
    private $servers = null; 
    
    /**
     * Constructor
     * Servers must be added manually to the Server Manager
     */
    public function ServerManager(){
        $this->servers = array();
        $this->servers["VMWARE"] = new Server("VMWARE","192.168.72.136", 2000, 2001);
        $this->servers["LOCAL"] = new Server("LOCAL","127.0.0.1", 2000, 2001);
    }
    
    /*
     * Prints an html table of the Execution Servers
     */
    public function printServers(){
        $list = "<pre><table border='0'><tr><td>Name</td><td>Url</td><td>Comm Port</td><td>Ftp Port</td></tr>";
        foreach($this->servers as $server){
            $list .= "<tr>";
            $list .= "<td>".$server->getName()."</td><td>".$server->getUrl()."</td><td>".$server->getCommPort()."</td><td>".$server->getFtpPort()."</td></tr>";   
        }
        $list .= "</table></pre>";
        return $list; 
    }
    
    /*
     * Returns an array of names that can be used to access 
     * elements in the server array
     */
    public function getServerList(){
        $list = array();
        foreach($this->servers as $server){
            $list[$i] = $server->getName();
        }
        return $list;
    }
    
    /*
     * Checks if a server is avaialable
     * input: 
     */
    public function isAvailable($serverName){
        $this->servers[$serverName]->checkAvailability();
    }
    
    public function getAvailableServers(){
       $list = array();
       $index = 0;
       foreach($this->servers as $server){
           if(@$server->checkAvailability() == true){
               $list[$index] = $server->getName();
               $index++;
           }
       }
       return $list;
    }
    
    public function getGPUServers(){
       $list = array();
       $index = 0;
       foreach($this->servers as $server){
           if($server->hasGPU() == true){
               $list[$index] = $server->getName();
               $index++;
           }
       }
       return $list;
    }
    
    public function getPublicServers(){
        $list = array();
        $index = 0;
        foreach($this->servers as $server){
           if($server->isPublic() == true){
               $list[$index] = $server->getName();
               $index++;
           }
       }
       return $list;
    }
    
    public function getJobStatus($serverName, $jobID){
        return $this->servers[$serverName]->getJobStatus($jobID);
    }
    
    public function getServerStatus($serverName){
        return $this->servers[$serverName]->getStatus();
    }

    public function createJob($serverName, $jobID, $xml, $inputFiles = null){
        $this->servers[$serverName]->createJob($jobID, $xml, $inputFiles);
    }
    
    public function getJobResults($serverName, $userID, $jobID){
        $this->servers[$serverName]->downloadResultFiles($userID, $jobID);
    }
    
    private function executeJob($serverName,$jobID){
        $this->servers[$serverName]->executeJob($jobID);
    }
}
?>
